import http from '@/utils/http'

export default {
  namespaced: true,
  state: {
    cinemaList: [],
    areaList: []
  },
  mutations: {
    changeCinema (state, data) {
      state.cinemaList = data
    },
    clearCinema (state) {
      state.cinemaList = []
    },
    changeAreaList (state, area) {
      state.areaList = area
    }
  },
  actions: {
    getCinemaData (store, params) {
      return http({
        // url: `/gateway?cityId=${store.state.cityId}&ticketFlag=1&k=2593828`,
        url: `/gateway?cityId=${params.cityId}&ticketFlag=${params.ticketFlag}&k=2593828`,
        headers: {
          'X-Host': 'mall.film-ticket.cinema.list'
        }
      }).then(response => {
        // console.log(response.data.data.cinemas)
        var repeatAreaList = response.data.data.cinemas.map(item => item.district)
        // console.log(repeatAreaList)
        // 数组去重
        for (var i = 0; i < repeatAreaList.length; i++) {
          for (var j = i + 1; j < repeatAreaList.length; j++) {
            if (repeatAreaList[i].districtId === repeatAreaList[j].districtId) {
              repeatAreaList.splice(j, 1)
              j--
            }
          }
        }
        // console.log(repeatAreaList)
        store.commit('changeAreaList', repeatAreaList)

        // 进行修改操作务必通过mutations，可以被监听
        store.commit('changeCinema', response.data.data.cinemas)
      })
    }
  }
}
